/**
 * Package:com.afteryuan.util.code;
 * $Id$
 * Copyright(c) 2001-2009 www.afteryuan.com
 */
package com.afteryuan.util.code;

import com.afteryuan.util.FileUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * ServiceImplGenerator
 * <p/>
 * <p><a href="ServiceImplGenerator.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@gmail.com">$Author$</a>
 * @version $Revision$
 */
public class ServiceImplGenerator extends ServiceGenerator {
        public static final String SINGLE_PACKAGENAME="impl";
    public static final String CLASS_STUFFIX_NAME="Impl";
    private File serviceImplFile;
    private String serviceImplPackageName;
    private String serviceImplClassName;

    /**
     * 构造器
     *
     * @param model   领域模型文件
     * @param srcRoot 源代码的根目录
     */
    protected ServiceImplGenerator(File model, File srcRoot) {
        super(model, srcRoot);
        serviceImplPackageName = servicePackageName+"."+SINGLE_PACKAGENAME;
        serviceImplClassName = serviceClassName+CLASS_STUFFIX_NAME;
        serviceImplFile = new File(model.getParent().replaceAll(MODE_SINGLE_PACKAGENAME,SERVICE_SINGLE_PACKAGENAME)+File.separator+SINGLE_PACKAGENAME, serviceImplClassName + ".java");
    }

    @Override
    public void generate() throws IOException {
        if(serviceImplFile.exists()&&!isReplace)return;
        FileUtil.createFile(serviceImplFile.getPath());
        FileWriter fw = new FileWriter(serviceImplFile);
        fw.write("/**\n" +
                " * Package:" + serviceImplPackageName + ";\n" +
                " * $Id$\n" +
                " * Copyright(c) 2001-2005 www.afteryuan.com\n" +
                " */\n");

        fw.write("package " + serviceImplPackageName + ";\n\n");
        fw.write("import " + modelFQClassName+";\n");
        fw.write("import " + serviceFQClassName + ";\n");
        fw.write("import com.afteryuan.dao.HibernateEntityDao;\n");
        fw.write("import org.apache.commons.logging.Log;\n");
        fw.write("import org.apache.commons.logging.LogFactory;\n\n");
        fw.write("/**\n" +
                " * " + serviceImplClassName + "\n" +
                " * <p/>\n" +
                " * <p><a href=\"" + serviceImplClassName + ".java.html\"><i>View Source</i></a></p>\n" +
                " *\n" +
                " * @author <a href=\"mailto:afteryuan@gmail.com\">$" + "Author" + "$</a>\n" +
                " * @version $" + "Revision" + "$\n" +
                " */\n");
        fw.write("public class " + serviceImplClassName + " extends HibernateEntityDao<" + modelClassName + "> implements " + serviceClassName + " {\n" +
                "\n" +
                "\tprivate static Log log = LogFactory.getLog(" + serviceImplClassName + ".class);\n" +
                "}");
        fw.close();
        System.out.println("generate file"+serviceImplFile);
    }
}
